package com.yutel.multher.jas;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;

import com.yutel.multher.JasMain;

public class Launcher {

	private static JasLog log = JasLog.getLogger(JasMain.class);

	private LifecycleManager lifecycle;

	public Launcher() {
		init();
	}

	public void start() throws Exception {
		lifecycle.start();
	}

	public void stop() throws Exception {
		lifecycle.stop();
	}

	private void init() {
		try {
			String launcherHome = getLauncherHome();
			log.info("launcher.home is " + launcherHome);

			JasContext config = getConfiguration(launcherHome);
			log.info("launcher.properties is " + config);

			String applicationHome = getApplicationHome(config, launcherHome);
			log.info("application.home is " + applicationHome);

			lifecycle = new LifecycleManager(config);
		} catch (Exception e) {
			log.error("launcher init error.", e);
			throw new RuntimeException(e);
		}
	}

	/**
	 * Set the <code>launcher.home</code> System property to the current working
	 * directory if it has not been set.
	 * 
	 * @return
	 */
	private String getLauncherHome() {
		String launcherHome = System.getProperty(JasContext.LAUNCHER_HOME);
		if (launcherHome != null) {
			return launcherHome;
		}

		String userDir = System.getProperty("user.dir");
		try {
			File parentDir = new File(userDir, "..");
			System.setProperty(JasContext.LAUNCHER_HOME,
					parentDir.getCanonicalPath());
		} catch (IOException e) {
			log.error("get home path error", e);
			File binDir = new File(userDir);
			System.setProperty(JasContext.LAUNCHER_HOME, binDir.getPath());
		}
		return System.getProperty(JasContext.LAUNCHER_HOME);
	}

	private JasContext getConfiguration(String launcherHome) {
		// first find launcher file from system property
		InputStream is = null;
		try {
			String configUrl = System.getProperty(JasContext.JAS_CONFIG_KEY);
			if (configUrl != null) {
				is = (new URL(configUrl)).openStream();
			}
		} catch (Throwable t) {
		}

		// second find launcher file from launcher home's dir
		if (is == null) {
			try {
				File confidDir = new File(launcherHome,
						JasContext.JAS_CONFIG_PATH);
				File configFile = new File(confidDir,
						JasContext.JAS_CONFIG_FILE);
				is = new FileInputStream(configFile);
			} catch (Throwable t) {
			}
		}

		JasContext configuration = new JasContext();
		if (is != null) {
			try {
				configuration.loadProperties(is);
			} catch (Throwable t) {
				log.error("Failed to load launcher.properties.", t);
			} finally {
				try {
					is.close();
				} catch (IOException e) {
				}
			}
		}

		return configuration;
	}

	private String getApplicationHome(JasContext config, String launcherHome) {
		String applicationHome = System
				.getProperty(JasContext.APPLICATION_HOME);
		if (applicationHome != null && applicationHome.length() > 0) {
			config.setProperty(JasContext.APPLICATION_HOME, applicationHome);
			return applicationHome;
		}

		applicationHome = config.getProperty(JasContext.APPLICATION_HOME);
		if (applicationHome != null && applicationHome.length() > 0) {
			System.setProperty(JasContext.APPLICATION_HOME, applicationHome);
			return applicationHome;
		}

		applicationHome = launcherHome;
		System.setProperty(JasContext.APPLICATION_HOME, applicationHome);
		config.setProperty(JasContext.APPLICATION_HOME, applicationHome);
		return applicationHome;
	}

}
